import axios from 'axios';

interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

interface DoctorListParams {
  page: number;
  size: number;
  department?: string;
  name?: string;
}

export interface Doctor {
  id?: number;
  doctorName: string;
  photo: string;
  hospitalArea: string;
  department: string;
  position: string;
  status: boolean;
}

export const fetchDoctors = async (params: DoctorListParams) => {
  const res = await axios.get<ApiResponse<{ data: Doctor[]; total: number }>>('/api/doctors', { params });
  return res.data;
};

export const fetchDoctorById = async (id: number) => {
  const res = await axios.get<ApiResponse<Doctor>>(`/api/doctors/${id}`);
  return res.data.data;
};

export const saveDoctor = async (doctor: Doctor) => {
  return doctor.id 
    ? axios.put(`/api/doctors/${doctor.id}`, doctor)
    : axios.post('/api/doctors', doctor);
};

export const deleteDoctor = async (id: number) => {
  return axios.delete(`/api/doctors/${id}`);
};